package cn.com.ghy.android.widget.report;

import java.io.Serializable;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import cn.com.ghy.android.bean.LoginInfoBean;
import cn.com.ghy.android.exception.DisposeException;
import cn.com.ghy.android.service.OrderOpeService;
import cn.com.ghy.android.util.CommonStatic;
import cn.com.ghy.android.widget.ComStaticApp;
import cn.com.ghy.android.widget.R;

/**
 * @软件著作权:金红叶纸业集团有限公司
 * @系统名称:SFA系统
 * @Description:订单销售月报表查询
 * @author wuz  @date 2011-5-23 08:45:12
 * @版本历史:v1.0
 * @Copyright:Copyright (c) 2011-2012 
 */

public class OrderSellReport extends Activity {

	private Spinner spiYear = null;
	private Spinner spiMonth = null;
	private Button btnSelect = null;
	private ListView listOrderSellReport = null;
	private ProgressDialog progressDialog = null;

	private Bundle bundle = null;

	private LoginInfoBean loginInfo = null;

	private String yearStr = "";
	private String monthStr = "";
	private String monthid = "";

	private HttpThread thread = null;

	private DisposeException disposeException = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.order_sell_report);
		getSession();
		findView();
		init();
		addListener();
	}

	private void getSession() {
		disposeException = ((ComStaticApp) getApplication()).getDisposeException();
		loginInfo = ((ComStaticApp) this.getApplication()).getLoginInfo();
	}

	private void findView() {
		spiYear = (Spinner) this.findViewById(R.id.spiYear);
		spiMonth = (Spinner) this.findViewById(R.id.spiMonth);
		btnSelect = (Button) this.findViewById(R.id.btnSelect);
		listOrderSellReport = (ListView) this.findViewById(R.id.listOrderSellReport);
	}

	private void init() {
		spiYear.setAdapter(GetSpiYearAdapter());
		spiMonth.setAdapter(GetSpiMonthAdapter());
	}

	private void addListener() {
		spiYear.setOnItemSelectedListener(new OnItemSelectedListener() {

			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
				yearStr = parent.getItemAtPosition(position).toString();
			}

			public void onNothingSelected(AdapterView<?> parent) {
			}

		});

		spiMonth.setOnItemSelectedListener(new OnItemSelectedListener() {

			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
				monthStr = parent.getItemAtPosition(position).toString();
			}

			public void onNothingSelected(AdapterView<?> parent) {
			}
		});

		btnSelect.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				monthid = yearStr + monthStr;
				thread = new HttpThread(handler);
				thread.doStart();
			}
		});
	}

	private ArrayAdapter<Integer> GetSpiYearAdapter() {
		Integer years[] = new Integer[5];
		Calendar c = Calendar.getInstance();
		c.add(GregorianCalendar.YEAR, 1);
		for (int i = 0; i < years.length; i++) {
			c.add(GregorianCalendar.YEAR, -1);
			years[i] = c.get(Calendar.YEAR);
		}
		ArrayAdapter<Integer> yearAdapter = new ArrayAdapter<Integer>(OrderSellReport.this,
				android.R.layout.simple_spinner_item, years);
		yearAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		return yearAdapter;
	}

	private ArrayAdapter<String> GetSpiMonthAdapter() {
		String months[] = new String[] { "01", "02", "03", "04", "05", "06", "07", "08", "09",
				"10", "11", "12" };
		ArrayAdapter<String> monthAdapter = new ArrayAdapter<String>(OrderSellReport.this,
				android.R.layout.simple_spinner_item, months);
		monthAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		return monthAdapter;
	}

	private class OrderSellReportListAdapter extends BaseAdapter {

		private Context context = null;
		private List<String[]> list = null;

		public OrderSellReportListAdapter(Context context, List<String[]> list) {
			this.context = context;
			this.list = list;
		}

		@Override
		public int getCount() {
			return list.size();
		}

		@Override
		public String[] getItem(int position) {
			return list.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			String inflater = Context.LAYOUT_INFLATER_SERVICE;
			LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(inflater);
			LinearLayout linearLayout = (LinearLayout) layoutInflater.inflate(
					R.layout.order_sell_report_lay, null);

			String[] str = list.get(position);
			TextView tv = (TextView) linearLayout.findViewById(R.id.labTxtInfoName);
			tv.setText(str[0]);
			tv = (TextView) linearLayout.findViewById(R.id.labTxtDetailInfo);
			tv.setText(str[1]);

			return linearLayout;
		}

	}

	private class HttpThread extends Thread {
		private Handler handle = null;

		public HttpThread(Handler hander) {
			handle = hander;
		}

		public void doStart() {
			progressDialog = new ProgressDialog(OrderSellReport.this);
			progressDialog.setTitle("提示信息");
			progressDialog.setMessage("正在请求数据，请稍等......");
			progressDialog.setCancelable(false);
			progressDialog.show();
			this.start();
		}

		public void run() {
			Message message = null;
			try {
				List<String[]> list = null;
				OrderOpeService orderOpeService = new OrderOpeService();
				list = orderOpeService.RpSaleList(monthid, loginInfo);
				if (null != list && list.size() > 0) {
					progressDialog.dismiss();
					message = handle.obtainMessage();
					bundle = new Bundle();
					message.what = CommonStatic.STATE_NORMAL;
					bundle.putSerializable("list", (Serializable) list);
					message.setData(bundle);
					handle.sendMessage(message);
				} else {
					progressDialog.dismiss();
					message = handle.obtainMessage();
					message.what = CommonStatic.STATE_NORMAL_NULL;
					handle.sendMessage(message);
				}
			} catch (Exception ex) {
				progressDialog.dismiss();
				disposeException.handleException(ex);
			}
		}
	}

	private Handler handler = new Handler() {
		public void handleMessage(Message m) {
			switch (m.what) {
				case CommonStatic.STATE_NORMAL :
					@SuppressWarnings("unchecked")
					List<String[]> list = (List<String[]>) m.getData().getSerializable("list");
					OrderSellReportListAdapter adapter = new OrderSellReportListAdapter(
							OrderSellReport.this, list);
					listOrderSellReport.setAdapter(adapter);
					break;
				case CommonStatic.STATE_NORMAL_NULL :
					new AlertDialog.Builder(OrderSellReport.this).setTitle("提示信息")
							.setMessage(yearStr + "年" + monthStr + "月份未开账,请确认后再查询!")
							.setNeutralButton("关闭", new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dlg, int sumthin) {
									listOrderSellReport.removeAllViewsInLayout();
									listOrderSellReport.invalidate();
								}
							}).show();
					break;
			}
		}
	};
}
